//
//  UserEditNameController.m
//  CheekiOS
//
//  Created by mac on 2017/6/17.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserEditNameController.h"
#import "HSLimitText.h"
@interface UserEditNameController ()<HSLimitTextDelegate>


@property (nonatomic, strong) HSLimitText *nickNameTextField;  ///< textField
@property (nonatomic, strong) UILabel *labTextFieldCount;//字数限制
@property (nonatomic, strong) NSString *strLab;
@end

@implementation UserEditNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _titleStr;
    self.view.backgroundColor = Theme_Color_White;
    _strLab = @"";
    UIButton *LeftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [LeftBtn addTarget:self action:@selector(LeftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [LeftBtn setTitle:@"取消" forState:UIControlStateNormal];
    LeftBtn.frame = CGRectMake(0, 0, 40, 40);
    [LeftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    LeftBtn.titleLabel.font = FontP(15);
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:LeftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;

    _nickNameTextField = [[HSLimitText alloc] init];
    _nickNameTextField.type =TextInputTypeTextfield;
    _nickNameTextField.delegate = self;
    if ([_typeStr isEqualToString:@"0"]) {
        _nickNameTextField.placeholder = @"请输入昵称";
    }else{
        _nickNameTextField.placeholder = @"请输入个性签名";
        
    }

    if ([_typeStr isEqualToString:@"0"]) {
        _nickNameTextField.maxLength = 8;

    }else{
        _nickNameTextField.maxLength = 32;

    }
    _nickNameTextField.isBecomFirstResponder = YES;
    
    [self.view addSubview:_nickNameTextField];
    
    [_nickNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(self.view).offset(18.5);
        make.height.mas_equalTo(40);
    }];
    
    _labTextFieldCount =[UILabel new];
    if ([_typeStr isEqualToString:@"0"]) {
        _labTextFieldCount.text = @"您最多还能输入8个字";
    }else{
        _labTextFieldCount.text = @"您最多还能输入32个字";

    }
    _labTextFieldCount.font = FontBold(15);
    _labTextFieldCount.textColor = RGB(175, 177, 180);
    _labTextFieldCount.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:_labTextFieldCount];
    [_labTextFieldCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(KScreenWidth);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(_nickNameTextField.mas_bottom).offset(80);
        make.height.mas_equalTo(40);
    }];
    
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame =CGRectMake(25,266, KScreenWidth-50, 50);
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    btn.layer.cornerRadius=25;
    btn.backgroundColor = Colorbackground;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(imageUpdateImageStr) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
}

- (void)imageUpdateImageStr{
    if(_strLab.length==0){
        [SVProgressHUD showErrorWithStatus:@"昵称名不能为空!"];
        return ;
    }
    NSDictionary *dict = [NSDictionary dictionary];
  
    if ([_typeStr isEqualToString:@"0"]) {
                    dict = @{
                               @"uid":[HQHttpManager getUserId],
                               @"nickName":_strLab
                               };
    }else{
        dict = @{
                 @"uid":[HQHttpManager getUserId],
                 @"motto":_strLab
                 };
    }
    
       [HcardHttpmanager MakePersionalInfoWithParam:dict success:^(id sucess) {
        if ([sucess[@"code"] isEqualToString:@"ok"]) {
            NSString *UserEditNameUpdateNotifiction = @"UserEditNameUpdateNotifiction";
            [[NSNotificationCenter defaultCenter] postNotificationName:UserEditNameUpdateNotifiction object:nil];         

            [self.navigationController popViewControllerAnimated:YES];
   
        }
        
      
        
    } failure:^(NSError *error) {
        
    }];

    
}

- (void)LeftBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)limitTextLimitInputOverStop:(HSLimitText *)textLimitInput
{

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"超出字数" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:confirAction];
    [self.navigationController presentViewController:alertVC animated:YES completion:nil];
    
}

- (void)limitTextLimitInput:(HSLimitText *)textLimitInput text:(NSString *)text
{
    _strLab = text;

    if ([_typeStr isEqualToString:@"0"]) {
        if([textLimitInput isEqual:self.nickNameTextField]){
            
            self.labTextFieldCount.text = [NSString stringWithFormat:@"您最多还能输入%d个字",(8-(int)text.length)];
        }
    }else{
        if([textLimitInput isEqual:self.nickNameTextField]){
            
            self.labTextFieldCount.text = [NSString stringWithFormat:@"您最多还能输入%d个字",(32-(int)text.length)];
        }
    }
 
   
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
